/*
 * Copyright 2011 Kim Lindhardt Madsen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package dk.lindhardt.gwt.geie.client.edit;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;
import dk.lindhardt.gwt.geie.client.image.GeieImages;
import dk.lindhardt.gwt.geie.shared.CellFormat;

import java.util.ArrayList;
import java.util.List;

/**
 * User: AnAmuser
 * Date: 19-08-11
 * <p/>
 * Styles vertical alignment and horizontal alignment
 */
public class AlignmentStyleMenu extends Composite implements StyleMenu {

   private static GeieImages imageResources = GWT.create(GeieImages.class);
   private MenuStateChangeHandler stateChangeHandler;

   private VerticalAlignmentMenu verticalAlignmentMenu;
   private HorizontalAlignmentMenu horizontalAlignmentMenu;

   public AlignmentStyleMenu(MenuStateChangeHandler stateChangeHandler, CellFormat cellFormat) {
      this.stateChangeHandler = stateChangeHandler;
      VerticalPanel mainPanel = new VerticalPanel();
      initWidget(mainPanel);
      verticalAlignmentMenu = new VerticalAlignmentMenu(cellFormat);
      verticalAlignmentMenu.addStyleName("geie-menu");
      verticalAlignmentMenu.addStyleName("geie-vertical-alignment-menu");
      horizontalAlignmentMenu = new HorizontalAlignmentMenu(cellFormat);
      horizontalAlignmentMenu.addStyleName("geie-menu");
      horizontalAlignmentMenu.addStyleName("geie-horizontal-alignment-menu");

      mainPanel.add(verticalAlignmentMenu);
      mainPanel.add(horizontalAlignmentMenu);
   }

   public void transferValues(CellFormat cellFormat) {
      verticalAlignmentMenu.transferValues(cellFormat);
      horizontalAlignmentMenu.transferValues(cellFormat);
   }

   private class HorizontalAlignmentMenu extends Composite implements StyleMenu {

      private ToggleButton left;
      private ToggleButton center;
      private ToggleButton right;

      private HorizontalAlignmentMenu(CellFormat cellFormat) {
         left = new ToggleButton(new Image(imageResources.textAlignLeft()));
         left.addStyleName("geie-toggle-button");
         center = new ToggleButton(new Image(imageResources.textAlignCenter()));
         center.addStyleName("geie-toggle-button");
         right = new ToggleButton(new Image(imageResources.textAlignRight()));
         right.addStyleName("geie-toggle-button");

         SingleSelectionToggleHandler toggleMenu = new SingleSelectionToggleHandler();
         toggleMenu.addButton(left);
         toggleMenu.addButton(center);
         toggleMenu.addButton(right);

         HorizontalPanel panel = new HorizontalPanel();
         initWidget(panel);
         panel.add(left);
         panel.add(center);
         panel.add(right);

         CellFormat.Alignment alignment = cellFormat.getAlignment();
         if (alignment != null) {
            switch (alignment) {
               case CENTER:
                  center.setDown(true);
                  break;
               case LEFT:
                  left.setDown(true);
                  break;
               case RIGHT:
                  right.setDown(true);
                  break;
            }
         }
      }

      public void transferValues(CellFormat cellFormat) {
         if (left.isDown()) {
            cellFormat.setAlignment(CellFormat.Alignment.LEFT);
         } else if (center.isDown()) {
            cellFormat.setAlignment(CellFormat.Alignment.CENTER);
         } else if (right.isDown()) {
            cellFormat.setAlignment(CellFormat.Alignment.RIGHT);
         }
      }
   }

   private class VerticalAlignmentMenu extends Composite implements StyleMenu {

      private ToggleButton top;
      private ToggleButton middle;
      private ToggleButton bottom;

      public VerticalAlignmentMenu(CellFormat cellFormat) {
         top = new ToggleButton(new Image(imageResources.vAlignTop()));
         top.addStyleName("geie-toggle-button");
         middle = new ToggleButton(new Image(imageResources.vAlignMiddle()));
         middle.addStyleName("geie-toggle-button");
         bottom = new ToggleButton(new Image(imageResources.vAlignBottom()));
         bottom.addStyleName("geie-toggle-button");

         SingleSelectionToggleHandler toggleMenu = new SingleSelectionToggleHandler();
         toggleMenu.addButton(top);
         toggleMenu.addButton(middle);
         toggleMenu.addButton(bottom);

         HorizontalPanel panel = new HorizontalPanel();
         initWidget(panel);
         panel.add(top);
         panel.add(middle);
         panel.add(bottom);

         CellFormat.VAlignment valignment = cellFormat.getValignment();
         if (valignment != null) {
            switch (valignment) {
               case BOTTOM:
                  bottom.setDown(true);
                  break;
               case CENTER:
                  middle.setDown(true);
                  break;
               case TOP:
                  top.setDown(true);
                  break;
            }
         }
      }

      public void transferValues(CellFormat cellFormat) {
         if (top.isDown()) {
            cellFormat.setValignment(CellFormat.VAlignment.TOP);
         } else if (middle.isDown()) {
            cellFormat.setValignment(CellFormat.VAlignment.CENTER);
         } else if (bottom.isDown()) {
            cellFormat.setValignment(CellFormat.VAlignment.BOTTOM);
         }
      }
   }

   private class SingleSelectionToggleHandler {

      private List<ToggleButton> buttons = new ArrayList<ToggleButton>();

      public void addButton(final ToggleButton button) {
         buttons.add(button);
         button.addClickHandler(
               new ClickHandler() {
                  public void onClick(ClickEvent event) {
                     boolean isAnyButtonDown = false;
                     for (ToggleButton toggleButton : buttons) {
                        if (button.isDown() && !toggleButton.equals(button)) {
                           toggleButton.setDown(false);
                        } else if (toggleButton.isDown()) {
                           isAnyButtonDown = true;
                        }
                     }
                     if (!isAnyButtonDown) {
                        button.setDown(true);
                     } else {
                        stateChangeHandler.onStyleChanged();
                     }
                  }
               });
      }

   }
}
